#!/usr/bin/ksh
#
# description: gmond startup script
#
# Jul 03, 2006, Michael Perzl (michael@perzl.org)
#

GMOND_BIN=/opt/freeware/sbin/gmond

PIDFILE=/var/run/gmond.pid

# Check for missing binaries (stale symlinks should not happen)
test -x ${GMOND_BIN} ||
    {
      echo "${GMOND_BIN} not installed"
      if [ "$1" = "stop" ] ; then
          exit 0
      else
          exit 5
      fi
    }

# Check for existence of needed config file and read it
GMOND_CONFIG=/etc/ganglia/gmond.conf
test -r ${GMOND_CONFIG} ||
    {
      echo "${GMOND_CONFIG} does not exist"
      if [ "$1" = "stop" ] ; then
          exit 0
      else
          exit 6
      fi
    }

RUN_AS_USER="nobody"

case "$1" in
    start)
        if [ -r ${PIDFILE} ] ; then
            echo "GANGLIA gmond daemon is already running with PID "`cat ${PIDFILE}`"."
            exit 1
        fi
        echo "Starting GANGLIA gmond..."

        ## Start daemon and write PID to file $PIDFILE.
        mkdir -p /var/run
        ${GMOND_BIN} -p ${PIDFILE} -c ${GMOND_CONFIG}
        ;;
    stop)
        echo "Shutting down GANGLIA gmond daemon... "
        ## Stop daemon.
        if [ -r ${PIDFILE} ] ; then
            kill -15 `cat ${PIDFILE}`
            rm -f ${PIDFILE}
        fi
        ;;
    status)
        if [ -r ${PIDFILE} ] ; then
            echo "GANGLIA gmond daemon is running with PID "`cat ${PIDFILE}`"."
        else
            echo "GANGLIA gmond daemon is not running."
        fi
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac

